package com.yuejian_06.yuejian_06.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.activity.CourseInfoActivity;
import com.yuejian_06.yuejian_06.bean.CourseBean;
import com.yuejian_06.yuejian_06.fragment.Constants;
import com.yuejian_06.yuejian_06.fragment.Constants_l;

import java.util.List;

/**
 * Created by songedwin on 2017/10/11.
 */

public class CourseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{


    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    public CourseAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.course_cardview, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final CourseBean entity = (CourseBean) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.course_name.setText(entity.getName());


        String internetUrl = Constants.SSS+"yuejian/Public/coursepics/"+entity.getPic().toString();
        if(!entity.getPic().isEmpty())
        {
            Picasso
                    .with(mContext)
                    .load(internetUrl)
                    .into(viewHolder.course_pic);}
        else
        {
            viewHolder.course_pic.setImageResource(R.drawable.club_wu);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, CourseInfoActivity.class);
                intent.putExtra("course_id",entity.getCourse_id());
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView course_pic;
        TextView course_name;


        public ViewHolder(View itemView) {
            super(itemView);
            course_name = (TextView) itemView.findViewById(R.id.course_rvname);
            course_pic = (ImageView) itemView.findViewById(R.id.course_rvpic);
        }
    }
}


